<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soa2Logs
 *
 * @author dxx
 */

namespace app\common\model\log;

use think\Model;

class PushMsgLogs extends Model {

    protected $table = 'jj_push_msg_logs';
    private $_baseTable = 'jj_push_msg_logs';
    protected $connection = ['database' => 'jooj_log'];
    
     public function __construct($data = array()) {
        $date = date('Ymd', time());
        $this->table = $this->_baseTable . '_' . $date;
        parent::__construct($data);
    }
    
     private function _createTable() {
        $sql = 'CREATE TABLE ' . $this->table . ' LIKE ' . $this->_baseTable;
        return $this->execute($sql);
    }
    
    public function add($params) {

        $data = [
            'open_id'=>$params['open_id'],
            'app_id'=>$params['app_id'],
            'msg_type'=>$params['msg_type'],
            'xml'=>$params['xml'],
            'add_time'=>$params['add_time'],
        ];

        try {
            $this->save($data);
        } catch (\Exception $e) {
            if (strstr($e->getMessage(),'Base table or view not found')) {
                if($this->_createTable()){
                    $this->save($data);
                }
            }
        }
    }

}
